package com.potenza.training.resume.controller;

import com.aliyun.oss.OSS;
import com.aliyun.oss.model.ObjectMetadata;
import io.renren.common.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Decoder;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.Random;


/**
 * @author zengyang
 * @create 2021-05-06-11:33
 */

@RestController
@RequestMapping("resume/diction/file")
public class fileController {
    @Autowired
    private OSS oss;

    @Value("${oss.hostName}")
    private String hostName;

    @Value("${oss.bucket}")
    private String bucket;

    @PostMapping("uploadImg")
    public R testfile(MultipartFile file) throws IOException {

        String originalFilename = file.getOriginalFilename();

        oss.putObject(bucket, originalFilename, file.getInputStream());

        String imageurl = "https://" + bucket + "." + hostName + "/" + originalFilename;

        return R.ok().put("image",imageurl);
    }

    /*
    *    以下是保存批注图片的方法
    * */
    @RequestMapping(value = "/base64UploadImage", method = RequestMethod.POST)
    public R base64UploadImage(@RequestBody Map saveMap) {
        // 接收到前端传递的 base64字符串 编码
        String baseUrl = saveMap.get("baseUrl").toString();
        //去掉头部信息
        String imgdata = baseUrl.replace("data:image/jpeg;base64,","");
        //解码 并转为图片 保存本地
        R r= GenerateImage(imgdata);
        return r;
    }

    //base64字符串转化成图片
    public  R GenerateImage(String imgStr)
    {   //对字节数组字符串进行Base64解码并生成图片
        BASE64Decoder decoder = new BASE64Decoder();
        try
        {
            //Base64解码
            byte[] bytes = new BASE64Decoder().decodeBuffer(imgStr);  //将字符串转换为byte数组，这里的content是那一串base64密文 /9j/4AAQ.................，不包含（data:img/jpg;base64,）注意标点符号
            InputStream inputStream = new ByteArrayInputStream(bytes);
            //生成jpeg图片
            //日期+随机数 生成文件名字
            Date date = new Date();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
            String format = simpleDateFormat.format(date);
            Random random = new Random();
            int ends = random.nextInt(99);
            String.format("%02d",ends);//如果不足两位，前面补0
            //指定图片的路径
            String imgFilePath = format+ends+".jpg";//新生成的图片
            ObjectMetadata metadata = new ObjectMetadata();
            metadata.setContentLength(inputStream.available());

            oss.putObject(bucket, imgFilePath, inputStream,metadata);
            String imageurl = "https://" + bucket + "." + hostName + "/" + imgFilePath;
            return  R.ok().put("imageUrl",imageurl);
        }
        catch (Exception e)
        {
            return R.error();
        }
    }
}

